package com.ymatou.es.service;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.ymt.base.YmatouBaseCall;
import com.ymttest.utils.EnvSetup;

public class SocialfrontCall extends YmatouBaseCall{
	private static String opurl ="socialfront/item/_search";
	public SocialfrontCall() {
		super(EnvSetup.getSystemUrl("socialfront"),opurl,"GET","PARAM");
	}
	@Override
	public void addDefHeader() {
		client.addHeader("Accept","text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}
	
	/**
	 * 接口返回超时情况
	 * @return string 
	 * */
	public String getTimeoutStatus() {
		String timed_out = "true";
		try {
			JSONObject jo = new JSONObject(getReturnData());
			timed_out = jo.getString("timed_out");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return timed_out;
	}
	
	/**
	 * 获取索引里的返回数据
	 */
	public Object getHits(){
		Object list = null;
		try {
			JSONObject jsonsList = new JSONObject(getReturnData());
			list = jsonsList.getJSONObject("hits");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
	
	/**
	 * 获取索引版本
	 * */
	public String getIndex(){
		String indexString = "";
		String hits2String="";
		String index = "";
		String hits = this.getHits().toString();
		JSONObject hitsObject;
		try {
			hitsObject = new JSONObject(hits);
		
	  Object hits2 = hitsObject.get("hits");
	   hits2String = hits2.toString();
	   JSONArray  hits2Array = new JSONArray(hits2String);
	   JSONObject  hitsObject1 = hits2Array.getJSONObject(0);
	   index = hitsObject1.getString("_index");
	   
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return index;
	}
}
